//
//  HLGiftAttachment.m
//  YueYue
//
//  Created by hulu001 on 2023/6/12.
//

#import "HLGiftAttachment.h"

@implementation HLGiftAttachment


- (nonnull NSString *)encodeAttachment {
    NSDictionary *dict = @{
                           @"msgType" : @(4),
                           @"body" : @{
                                   @"orderId" : self.orderId,
                                   @"giftName" : self.giftName,
                                   @"giftNameVn" : self.giftNameVn,
                                   @"giftImgLink" : self.giftImgLink,
                                   @"giftAnimateLink" : self.giftAnimateLink
                                    }
                           };
    
    NSData *data = [NSJSONSerialization dataWithJSONObject:dict
                                                   options:0
                                                     error:nil];
    NSString *content = nil;
    if (data) {
        content = [[NSString alloc] initWithData:data
                                        encoding:NSUTF8StringEncoding];
    }
    return content;
}

- (CGSize)contentSize:(NIMMessage *)message cellWidth:(CGFloat)width
{
    NIMCustomObject *object = (NIMCustomObject *)message.messageObject;
    HLGiftAttachment *attachment = (HLGiftAttachment *)object.attachment;
    
    NSString *giftName = attachment.giftName;

    NSString *prefixName = @"";
    if ([message.from isEqualToString:HLRunTime.shareInstance.mine.userId]) {
        prefixName = @"送出礼物：";
    } else {
        prefixName = @"送你礼物：";
    }
    NSString *text = [NSString stringWithFormat:@"%@%@", prefixName,giftName];
    
    CGFloat textW = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, 22) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size.width;
    
    return CGSizeMake(66 + textW + 12, 60);
}

- (NSString *)cellContent:(NIMMessage *)message
{
    return @"HLGiftContentView";
}

- (UIEdgeInsets)cellInsets:(NIMMessage *)message
{
    // 要不要显示昵称 气泡位置分别设置
    if ([self shouldShowAvatar:message])
    {
        return UIEdgeInsetsMake(8, 58, 28, 56);
    }
    else
    {
        return UIEdgeInsetsMake(8, 58, 28, 56);
    }
}

- (UIEdgeInsets)contentViewInsets:(NIMMessage *)message
{
    return UIEdgeInsetsZero;
}

- (BOOL)shouldShowAvatar:(NIMMessage *)message
{
    return YES;
}


@end
